package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.ReturnDeposit;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.ReturnDepositService;
import com.tbit.uqbike.service.business.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 押金退还
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/returnDeposit", produces = BaseController.MEDIA_JSON_UTF8)
public class ReturnDepositController extends BaseController {
    @Autowired
    private ReturnDepositService returnDepositService;
    @Autowired
    private UserService userService;
    @Autowired
    private OperaPswService operaPswService;

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String phone,
                                @RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                Integer state, String startTime, String endTime,
                                HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        Integer accountId;
        if (loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            /* 校验 */
            accountId = null;
        } else if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        List<Integer> userIds = new ArrayList<>();
        if (phone != null) {
            /**查询用户*/
            List<User> users = userService.getByAccountIdAndPhone(accountId, phone);
            if (users == null || users.size() < 1) {
                throw new BaseException("ver.user.notNull");
            } else {
                for (User user : users) {
                    userIds.add(user.getUserId());
                }
            }
        }
        /*获取总数*/
        Integer total = returnDepositService.queryCount(accountId, state, userIds, startTime, endTime);
        /*当前返回行数*/
        List<ReturnDeposit> returnDeposits = returnDepositService.queryPage(accountId,
                state, userIds, startTime, endTime, rowCount, pageNO);

        return JsonResult.ok(returnDeposits, total);
    }

    /* 人工处理押金 */
    @RequestMapping("/handleRecharge")
    @OperateLog(module = ModuleEnum.DEPOSIT, methods = MethodEnum.DEPOSIT_RETURN)
    public JsonResult handleByMan(@NotNull(message = "ver.userId.notNull") Integer userId,
                                  @NotNull(message = "ver.remark.notNull") String remark, String secret,
                                  HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        /* 校验 */
        if (!UserConstant.USER_ADMIN.equals(loginInfo.type) && !UserConstant.USER_BRAND.equals(loginInfo.type)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }

        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(), secret);

        returnDepositService.manualDepositRefund(userId, remark, new Date(),loginInfo.id,loginInfo.phone,loginInfo.name);

        return JsonResult.ok();
    }
}